/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.oauth2.domain.OauthClientDetails;
import cn.ibizlab.core.oauth2.service.OauthClientDetailsService;
import cn.ibizlab.core.oauth2.filter.OauthClientDetailsSearchContext;

/**
 * 实体[OauthClientDetails] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractOauthClientDetailsResource {

    @Autowired
    public OauthClientDetailsService oauthClientDetailsService;

    @Autowired
    @Lazy
    public OauthClientDetailsDTOMapping oauthClientDetailsDtoMapping;

    @Autowired
    @Lazy
    public OauthClientDetailsFilterDTOMapping oauthClientDetailsFilterDtoMapping;


    /**
    * 创建Create oauth客户端信息
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OauthClientDetailsDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Create-all')")
    @PostMapping(value = {"oauth_client_details","oauth_client_details"})
    public ResponseEntity<OauthClientDetailsDTO> create
            (@Validated @RequestBody OauthClientDetailsDTO dto) {
        OauthClientDetails domain = oauthClientDetailsDtoMapping.toDomain(dto);
        oauthClientDetailsService.create(domain);
        OauthClientDetails rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(oauthClientDetailsDtoMapping.toDto(rt));
    }

    /**
    * 获取Get oauth客户端信息
    * 
    *
    * @param clientId clientId
    * @return ResponseEntity<OauthClientDetailsDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-Get ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Get-all')")
    @GetMapping("oauth_client_details/{clientId}")
    public ResponseEntity<OauthClientDetailsDTO> getByClientId
            (@PathVariable("clientId") String clientId) {
        OauthClientDetails rt = oauthClientDetailsService.get(clientId);
        return ResponseEntity.status(HttpStatus.OK).body(oauthClientDetailsDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove oauth客户端信息
    * 
    *
    * @param clientId clientId
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Remove-all')")
    @DeleteMapping("oauth_client_details/{clientId}")
    public ResponseEntity<Boolean> removeByClientId
            (@PathVariable("clientId") String clientId) {
        Boolean rt = oauthClientDetailsService.remove(clientId);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update oauth客户端信息
    * 
    *
    * @param clientId clientId
    * @param dto dto
    * @return ResponseEntity<OauthClientDetailsDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Update-all')")
    @PutMapping("oauth_client_details/{clientId}")
    public ResponseEntity<OauthClientDetailsDTO> updateByClientId
            (@PathVariable("clientId") String clientId, @Validated @RequestBody OauthClientDetailsDTO dto) {
        OauthClientDetails domain = oauthClientDetailsDtoMapping.toDomain(dto);
        domain.setClientId(clientId);
        oauthClientDetailsService.update(domain);
        OauthClientDetails rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(oauthClientDetailsDtoMapping.toDto(rt));
    }

    /**
    * Cache_evict oauth客户端信息
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OauthClientDetailsDTO>
    */
    @ApiOperation(value = "Cache_evict", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-Cache_evict ")
    @PostMapping("oauth_client_details/cache_evict")
    public ResponseEntity<OauthClientDetailsDTO> cacheEvict
            (@Validated @RequestBody OauthClientDetailsDTO dto) {
        OauthClientDetails domain = oauthClientDetailsDtoMapping.toDomain(dto);
        OauthClientDetails rt = oauthClientDetailsService.cacheEvict(domain);
        return ResponseEntity.status(HttpStatus.OK).body(oauthClientDetailsDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey oauth客户端信息
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-CheckKey ")
    @PostMapping("oauth_client_details/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody OauthClientDetailsDTO dto) {
        OauthClientDetails domain = oauthClientDetailsDtoMapping.toDomain(dto);
        Integer rt = oauthClientDetailsService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * GenerateClientSecret oauth客户端信息
    * 
    *
    * @param clientId clientId
    * @param dto dto
    * @return ResponseEntity<OauthClientDetailsDTO>
    */
    @ApiOperation(value = "GenerateClientSecret", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-GenerateClientSecret ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-GenerateClientSecret-all','uaa-OauthClientDetails-Get-all')")
    @PostMapping("oauth_client_details/{clientId}/generateclientsecret")
    public ResponseEntity<OauthClientDetailsDTO> generateClientSecretByClientId
            (@PathVariable("clientId") String clientId, @Validated @RequestBody OauthClientDetailsDTO dto) {
        OauthClientDetails domain = oauthClientDetailsDtoMapping.toDomain(dto);
        domain.setClientId(clientId);
        OauthClientDetails rt = oauthClientDetailsService.generateClientSecret(domain);
        return ResponseEntity.status(HttpStatus.OK).body(oauthClientDetailsDtoMapping.toDto(rt));
    }

    /**
    * 草稿GetDraft oauth客户端信息
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OauthClientDetailsDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-GetDraft ")
    @GetMapping("oauth_client_details/getdraft")
    public ResponseEntity<OauthClientDetailsDTO> getDraft
            (@SpringQueryMap OauthClientDetailsDTO dto) {
        OauthClientDetails domain = oauthClientDetailsDtoMapping.toDomain(dto);
        OauthClientDetails rt = oauthClientDetailsService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(oauthClientDetailsDtoMapping.toDto(rt));
    }

    /**
    * 保存Save oauth客户端信息
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OauthClientDetailsDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Save-all')")
    @PostMapping("oauth_client_details/save")
    public ResponseEntity<OauthClientDetailsDTO> save
            (@Validated @RequestBody OauthClientDetailsDTO dto) {
        OauthClientDetails domain = oauthClientDetailsDtoMapping.toDomain(dto);
        oauthClientDetailsService.save(domain);
        OauthClientDetails rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(oauthClientDetailsDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault oauth客户端信息
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<OauthClientDetailsDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"oauth客户端信息" },  notes = "OauthClientDetails-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-FetchDefault-all')")
    @PostMapping("oauth_client_details/fetchdefault")
    public ResponseEntity<List<OauthClientDetailsDTO>> fetchDefault
            (@Validated @RequestBody OauthClientDetailsFilterDTO dto) {
        OauthClientDetailsSearchContext context = oauthClientDetailsFilterDtoMapping.toDomain(dto);
        Page<OauthClientDetails> domains = oauthClientDetailsService.searchDefault(context) ;
        List<OauthClientDetailsDTO> list = oauthClientDetailsDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建oauth客户端信息
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Create-all')")
    @ApiOperation(value = "批量新建oauth客户端信息", tags = {"oauth客户端信息" },  notes = "批量新建oauth客户端信息")
	@PostMapping("oauth_client_details/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<OauthClientDetailsDTO> dtos) {
        oauthClientDetailsService.createBatch(oauthClientDetailsDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除oauth客户端信息
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Remove-all')")
    @ApiOperation(value = "批量删除oauth客户端信息", tags = {"oauth客户端信息" },  notes = "批量删除oauth客户端信息")
	@DeleteMapping("oauth_client_details/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        oauthClientDetailsService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新oauth客户端信息
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Update-all')")
    @ApiOperation(value = "批量更新oauth客户端信息", tags = {"oauth客户端信息" },  notes = "批量更新oauth客户端信息")
	@PutMapping("oauth_client_details/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<OauthClientDetailsDTO> dtos) {
        oauthClientDetailsService.updateBatch(oauthClientDetailsDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存oauth客户端信息
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Save-all')")
    @ApiOperation(value = "批量保存oauth客户端信息", tags = {"oauth客户端信息" },  notes = "批量保存oauth客户端信息")
	@PostMapping("oauth_client_details/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<OauthClientDetailsDTO> dtos) {
        oauthClientDetailsService.saveBatch(oauthClientDetailsDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入oauth客户端信息
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OauthClientDetails-Save-all')")
    @ApiOperation(value = "批量导入oauth客户端信息", tags = {"oauth客户端信息" },  notes = "批量导入oauth客户端信息")
	@PostMapping("oauth_client_details/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<OauthClientDetailsDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(oauthClientDetailsService.importData(config,ignoreError,oauthClientDetailsDtoMapping.toDomain(dtos)));
    }

}
